<?php
include_once("../../../common/modules/checkA.php");
include_once("../../../../config.php");
?>




<?

$check_mode_number=50;//设置触发延迟发送的人数

print_r2($_REQUEST);

//获取当前优惠券的信息

$coupon=select_one_line_all_items($table_prefix."coupon","nid = '".$_REQUEST['coupon_nid']."'");

//如果选择优惠券，就提示没有优惠券
if(!$coupon['nid'])
{
    ?>
    <script type="text/javascript">
        alert("选择优惠券失败:未选择优惠券");
        document.location.href = "coupon-send.php?nowpageid=<? echo $nowpageid ?>&sortby=<? echo $sortby?>&nid=<? echo $_REQUEST['coupon_nid'] ?>&sys_part=<? echo $system_part ?>&tag=<? echo $_REQUEST['tag'] ?>"
    </script>

    <?php
    exit;
}


//优惠券数量，要大于0
if($_REQUEST['coupon_num'] < 1)
{
    ?>
    <script type="text/javascript">
        alert("派发优惠券数量不能为0");
        document.location.href = "coupon-send.php?nowpageid=<? echo $nowpageid ?>&sortby=<? echo $sortby?>&nid=<? echo $_REQUEST['coupon_nid'] ?>&sys_part=<? echo $system_part ?>&tag=<? echo $_REQUEST['tag'] ?>"
    </script>

    <?php
    exit;
}

//print_r2($coupon);







//如果没有选中用户，就提示没有派券成功
if((!$_REQUEST['member']) && ($_REQUEST['choose_mode'] !="整站"))
{
?>
    <script type="text/javascript">
        alert("派券失败:未选择用户");
        document.location.href = "coupon-send.php?nowpageid=<? echo $nowpageid ?>&sortby=<? echo $sortby?>&coupon_nid=<? echo $_REQUEST['coupon_nid'] ?>&sys_part=<? echo $system_part ?>&tag=<? echo $_REQUEST['tag'] ?>"
    </script>

<?php
exit;
}


//所有参数正常，开始派发优惠券。
else
{
echo "所有参数正常，开始派发优惠券<br/>";
echo $_REQUEST['choose_mode']."<br/>";




    //如果是全站模式，就直接把所有人的openid都插入一次到表格
    if($_REQUEST['choose_mode']=="整站")
    {
        $all_member_nid=select_lines_items($table_Prefix."member","nid asc","nid","1=1","");
        //print_r2($all_member_nid);


        echo "整站用户派券，插入coupon_send_list<br/>";


        //内嵌好數據的数据
        foreach ($all_member_nid as $k=>$v)
        {
            $_delay_list[$k]['member']        = $v['nid'];
            $_delay_list[$k]['coupon_number'] = $_REQUEST['coupon_num'];
            $_delay_list[$k]['coupon_nid']    = $_REQUEST['coupon_nid'];
            $_delay_list[$k]['add_date']      = date("Y-m-d H:i:s");
            $_delay_list[$k]['author']        = $_SESSION['nid'];
            $_delay_list[$k]['ip']            = $_SERVER["REMOTE_ADDR"];
        }
        //记录member，记录优惠券，记录优惠券数量，记录加入时间，记录处理时间
        $p['table']  = $table_Prefix . "coupon_send_list";
        $p['items']  = array("member_nid", "coupon_number", "coupon_nid", "add_date",'author','ip');
        $p['values'] = $_delay_list;
        $_delay_list_sql=insert_datas($p);
        $query = do_query($_delay_list_sql) or die ("error:" . mysqli_error().$_delay_list_sql);
        echo "延迟模式数据插入完毕。<br/>";
    }




//如果是勾选模式的话，就走这个
else
{
   if(count($_REQUEST['member']) > $check_mode_number)
   {
       echo "进入延迟模式";

       //内嵌好數據的数据
       foreach ($_REQUEST['member'] as $k=>$v)
       {
           $_delay_list[$k]['member']        = $v;
           $_delay_list[$k]['coupon_number'] = $_REQUEST['coupon_num'];
           $_delay_list[$k]['coupon_nid']    = $_REQUEST['coupon_nid'];
           $_delay_list[$k]['add_date']      = date("Y-m-d H:i:s");
           $_delay_list[$k]['author']        = $_SESSION['nid'];
           $_delay_list[$k]['ip']            = $_SERVER["REMOTE_ADDR"];
       }



       //记录member，记录优惠券，记录优惠券数量，记录加入时间，记录处理时间
       $p['table']  = $table_Prefix . "coupon_send_list";
       $p['items']  = array("member_nid", "coupon_number", "coupon_nid", "add_date",'author','ip');
       $p['values'] = $_delay_list;
       $_delay_list_sql=insert_datas($p);
       $query = do_query($_delay_list_sql) or die ("error:" . mysqli_error().$_delay_list_sql);
       echo "延迟模式数据插入完毕。";
       exit;
   }
   else
   {
       echo "进入即时模式";

        foreach ($_REQUEST['member'] as $k=>$v)
        {

            echo "<br/>".$v."应该要有一张这个的优惠券<br/>";


            $insert_data                   = [];
            $insert_data['ai_name']        = $coupon['name'];//优惠券名字
            $insert_data['ai_remark']      = $coupon['remark'];//优惠券备注
            $insert_data['ai_coupon_type'] = $coupon['coupon_type'];//优惠券类型
            $insert_data['ai_is_work']     = "1";//优惠券是否激活


            //这个优惠券是折扣还是定额
            if ($coupon['coupon_type'] == 10 || $coupon['coupon_type'] == 40 )
            {
            $insert_data['ai_reduce_price'] = $coupon['reduce_price'];
            }
            else
            {
            $insert_data['ai_discount'] = $coupon['discount'];
            }


            //这里应该要根据优惠券的到期类型去到做判断。到底是+日数，还是固定到期时间。
            if($coupon['end_time']>0)
            {
            $expire_day=$coupon['end_time'];
            }
            else
            {
            $expire_day=date("Y-m-d H:i:s",strtotime("+".$coupon['expire_day']." day "));
            }


            $insert_data['ai_end_time']  = strtotime($expire_day);
            $insert_data['ai_user_id']     = $v;
            $insert_data['ai_coupon_id']   = $coupon['nid'];
            $insert_data['ai_is_free']     = $coupon['is_free'];
            $insert_data['ai_create_time'] = time();//派发时间



            if($insert_data['ai_coupon_id'])
            {

                for ($i=1; $i<=$_REQUEST['coupon_num']; $i++)
                {
                $sql = insert_date($table_Prefix . 'coupon_member', $insert_data);
                $query = do_query($sql) or die ("error:" . mysqli_error());

                }


                $_member        = select_one_line_all_items($table_prefix . "member", "nid = '" . $v . "'");
                $_wechat_member = select_one_line_all_items($table_prefix . "wechat_member", "unionid = '" . $_member['unionid'] . "'");


                //触发发券通知信息
                //curl 一个api地址，带上通知内容代号，被通知人，然后就触发了。
                echo $_url=$domain."/api/wechat_message/notice_coupon_sent.php?openid=".$_wechat_member['openid'];
                //有关注公众号，才发送通知。
                if($_wechat_member['openid'])
                {
                file_get_contents($_url);
                }

            }


        }
   }

}


//勾选模式结尾
}



?>


<script type="text/javascript">
     alert("输入信息已保存，点击确认返回继续操作.");
     document.location.href = "detail-list.php?sys_part=coupon"
</script>


